package com.nhncorp.pleiades.client;

import java.util.HashMap;
import java.util.Map;

import com.nhncorp.pleiades.conf.PleiadesConfiguration;

public class LockServiceSingletonFactory {
  private static Map<String, LockService> lockServices = new HashMap<String, LockService>();
  
  public static LockService getLockService(PleiadesUser userInfo) {    
    return getLockService(userInfo, new PleiadesConfiguration());
    
  }
  
  public static LockService getLockService(PleiadesUser userInfo, PleiadesConfiguration conf) {
    synchronized(lockServices) {
      if(!lockServices.containsKey(userInfo.getHostName())) {
        LockService lockService = LockServiceClientFactory.newInstance(userInfo, conf);
        lockServices.put(userInfo.getHostName(), lockService);
        
        return lockService;
      } else {
        return lockServices.get(userInfo.getHostName());
      }
    }
  }

  public static void close(String owner) {
    synchronized(lockServices) {
      if(lockServices.containsKey(owner)) {
        lockServices.get(owner).close();
        lockServices.remove(owner);
      }
    }
  }
}
